package com.dormitoryAdministratorSystem.ui.roommanage;

import com.dormitoryAdministratorSystem.entity.maintain.Maintain;
import com.dormitoryAdministratorSystem.entity.page.PageBean;
import com.dormitoryAdministratorSystem.entity.room.RoomBean;
import com.dormitoryAdministratorSystem.entity.student.StudentBean;
import com.dormitoryAdministratorSystem.service.maintain.impl.MaintainSerImpl;
import com.dormitoryAdministratorSystem.service.room.impl.RoomSerImpl;
import com.dormitoryAdministratorSystem.ui.MainFrame;
import com.dormitoryAdministratorSystem.util.BeansUtil;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoTable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RoomManagePanel extends JPanel {

	private MainFrame frame;

	private LovoTable roomTab;

	private LovoButton addRoomBtn;
	private LovoButton delRoomBtn;
	private LovoButton roomInfoBtn;
	private LovoButton roomEqpBtn;
	private JButton preBtn;
	private JButton nextBtn;
	private JLabel  pageLab;
	private JPanel queryPanel;
	private JLabel roomAddressLab;
	private JTextField roomAddressTxt;
	private LovoComboBox<String> roomStatCmb;
	private LovoComboBox<String> liveableCmb;
	private LovoComboBox<String> roomTypeCmb;
	private JButton queryBtn;

	private RoomSerImpl rsi = new RoomSerImpl();
	private Map<String,String> con = new HashMap<>();
	private PageBean<RoomBean> rPage = new PageBean<>();
	private MaintainSerImpl msi = new MaintainSerImpl();

	public RoomManagePanel(MainFrame frame) {
		this.frame = frame;
		this.setBackground(Color.WHITE);
		this.setLayout(null);
		this.initTable();
		this.initBtn();
		this.initQueryPanel();
	}

	private void initTable() {
		// TODO Auto-generated method stub
		this.roomTab = new LovoTable(this, new String[] { "房间地址", "可容纳人数",
				"已住人数", "房租", "房间状态", "房东", "房东电话", "房间类型" },
				new String[] {"address", "size", "peopleNo", "rent", "roomState", "hostName", "hostTel", "roomType"},// 数组中应该是对应属性的属性名
				"rId");// 填入唯一标示属性
		this.roomTab.setSizeAndLocation(0, 0, 800, 300);
		rPage = rsi.showRoomInfos(con);
		this.roomTab.updateLovoTable(rPage.getDates());
	}

	private void initBtn() {
		// TODO Auto-generated method stub

		this.preBtn = new JButton("上一页");
		this.preBtn.setBounds(5,320,80,20);
		this.nextBtn = new JButton("下一页");
		this.nextBtn.setBounds(235,320,80,20);
		this.pageLab = new JLabel("页数" + rPage.getCurPage() + "/" + rPage.getTolSize() + ",共" + rPage.getTolNum() + "条"
				+ "每页" + rPage.getPageSize() + "条");
		this.pageLab.setBounds(90,320,200,20);
		this.add(this.pageLab);
		this.add(this.preBtn);
		this.add(this.nextBtn);


		this.addRoomBtn = new LovoButton("添加房间", 15, 380, this);
		this.delRoomBtn = new LovoButton("删除房间", 160, 380, this);
		this.roomInfoBtn = new LovoButton("查看房间信息", 15, 450, this);
		this.roomEqpBtn = new LovoButton("查看房间设施", 160, 450, this);

		this.addRoomBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				new RoomAddDialog(RoomManagePanel.this.frame, RoomManagePanel.this.rsi);
				updatePage(con.get("address"),con.get("state"),con.get("type"),con.get("canLive"),
						"", "");
			}
		});

		this.delRoomBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int id = roomTab.getKeyByInt();

				if(id == -1){
					JOptionPane.showMessageDialog(null, "请先选择房间");
				}else {
					List<StudentBean> list = BeansUtil.getStudents(id);

					if(list.size() == 0) {
						int isSure = JOptionPane.showConfirmDialog(null, "确定要删除吗");
						if(isSure == 0) {
							rsi.deleteRoom(id);
							JOptionPane.showMessageDialog(null, "成功");
						}
					}else{
						new RoomDelDialog(RoomManagePanel.this.frame);
					}
				}
				updatePage(con.get("address"),con.get("state"),con.get("type"),con.get("canLive"),
						rPage.getCurPage()+"", "");
			}
		});

		this.roomInfoBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int id = roomTab.getKeyByInt();
				if(id == -1){
					JOptionPane.showMessageDialog(null, "请先选择房间");
				}else{
					new RoomInfoDialog(RoomManagePanel.this.frame, id);
				}
				updatePage(con.get("address"),con.get("state"),con.get("type"),con.get("canLive"),
						rPage.getCurPage()+"", "");
			}
		});

		this.roomEqpBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int id = roomTab.getKeyByInt();
				if(id == -1 ){
					JOptionPane.showMessageDialog(null, "请先选择房间");
				}else{
					new RoomEqpDialog(RoomManagePanel.this.frame, id);
				}
				updatePage(con.get("address"),con.get("state"),con.get("type"),con.get("canLive"),
						rPage.getCurPage()+"", "");
			}
		});

		//上一页
		this.preBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(rPage.getCurPage() == 1){
					JOptionPane.showMessageDialog(null,"没有上一页了");
				}else{
					updatePage(con.get("address"),con.get("state"),con.get("type"),con.get("canLive"),
							(rPage.getCurPage() - 1)+"", rPage.getPageSize()+"");
				}
			}
		});

		//下一页
		this.nextBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(rPage.getCurPage() == rPage.getTolSize()){
					JOptionPane.showMessageDialog(null,"没有下一页了");
				}else{
					updatePage(con.get("address"),con.get("state"),con.get("type"),con.get("canLive"),
							(rPage.getCurPage() + 1)+"", rPage.getPageSize()+"");
				}
			}
		});

	}

	private void initQueryPanel() {
		// TODO Auto-generated method stub
		this.queryPanel = new JPanel();
		this.queryPanel.setBorder(BorderFactory.createTitledBorder("查询房间信息"));
		this.queryPanel.setBackground(Color.WHITE);
		this.queryPanel.setLayout(null);
		this.queryPanel.setBounds(310, 310, 480, 180);
		this.add(this.queryPanel);

		this.roomAddressLab = new JLabel("房间地址");
		this.roomAddressLab.setBounds(20, 20, 60, 20);
		this.queryPanel.add(this.roomAddressLab);
		this.roomAddressTxt = new JTextField();
		this.roomAddressTxt.setBounds(100, 20, 120, 20);
		this.queryPanel.add(this.roomAddressTxt);

		ArrayList<String> statLst = new ArrayList<String>();
		statLst.add("不限");
		statLst.add("正常");
		statLst.add("损坏");
		this.roomStatCmb = new LovoComboBox<String>( "房间状态",statLst, 260, 20,
				this.queryPanel);

		ArrayList<String> liveLst = new ArrayList<String>();
		liveLst.add("不限");
		liveLst.add("可住房间");
		this.liveableCmb = new LovoComboBox<String>( "可住房间",liveLst, 20, 80,
				this.queryPanel);

		ArrayList<String> typeLst = new ArrayList<String>();
		typeLst.add("不限");
		typeLst.add("男生宿舍");
		typeLst.add("女生宿舍");
		this.roomTypeCmb = new LovoComboBox<String>("房间类型", typeLst, 260, 80,
				this.queryPanel);

		this.queryBtn = new JButton("查找");
		this.queryBtn.setBounds(220, 130, 80, 20);
		this.queryPanel.add(this.queryBtn);

		queryBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String address = roomAddressTxt.getText();
				String canLive = "";
				String state = "";
				String type = "";

				if("可住房间".equals(liveableCmb.getItem())){
					canLive = "canLive";
				}

				if(!"不限".equals(roomStatCmb.getItem())){
					state = roomStatCmb.getItem();
				}

				if(!"不限".equals(roomTypeCmb.getItem())){
					type = roomTypeCmb.getItem();
				}

				updatePage(address,state,type, canLive, "", "");
			}
		});
	}

	public void updatePage(String address, String state, String type, String canLive, String curPage, String pageSize){
		con.put("address", address);
		con.put("state", state);
		con.put("type", type);
		con.put("canLive", canLive);
		con.put("curPage", curPage);
		con.put("pageSize", pageSize);
		updateState();
		rPage = rsi.showRoomInfos(con);
		roomTab.updateLovoTable(rPage.getDates());
		pageLab.setText("页数" + rPage.getCurPage() + "/" + rPage.getTolSize() + ",共" + rPage.getTolNum() + "条"
				+ "每页" + rPage.getPageSize() + "条");
	}

	private void updateState(){
		List<RoomBean> rList = rsi.getAllRoom();
//		System.out.println(rList);
		for (RoomBean roomBean : rList) {
			if("已损坏".equals(roomBean.getRoomState())){
				boolean flag = true;
				List<Maintain> mList = msi.getMaintainInfos(roomBean.getRId());
				for (Maintain maintain : mList) {
					if("否".equals(maintain.getIsSolve())){
						flag = false;
						break;
					}
				}

				if(flag){
					rsi.changeRoomState("正常", roomBean.getRId());
				}
			}
		}
	}

}
